package com.lz.hotel_java.service;

import com.lz.hotel_java.entity.Booking;
import com.lz.hotel_java.entity.BookingRes;
import com.lz.hotel_java.entity.Customer;
import com.lz.hotel_java.mapper.BookingMapper;
import com.lz.hotel_java.mapper.CustomerMapper;
import com.lz.hotel_java.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BookingServiceImpl implements BookingService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BookingMapper bookingMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    @Transactional
    public boolean reserveRoom(Customer customer, Integer roomId) {
        // 1. 插入客户信息（如有必要，避免重复）
        Customer exist = customerMapper.findByIdCard(customer.getIdCard());
        Integer customerId;
        if (exist == null) {
            customerMapper.insert(customer);
            customerId = customer.getCustomerID();
        } else {
            customerId = exist.getCustomerID();
        }
        // 2. 新建Booking
        Booking booking = new Booking();
        booking.setBookingTime(new Date());
        booking.setCustomerID(customerId);
        booking.setRoomID(roomId);
        booking.setStatus("已预订");
        bookingMapper.insert(booking);
        // 3. 修改房间状态
        int updated = roomMapper.updateStatus("已预订", roomId);
        return updated > 0;
    }

    @Override
    public List<BookingRes> getAll() {
        List<Booking> bookings = bookingMapper.selectAll();
        List<BookingRes> bookingResList = new ArrayList<>();
        for (Booking booking : bookings) {
            BookingRes bookingRes = new BookingRes();
            Customer customer = customerMapper.selectById(booking.getCustomerID());
            bookingRes.setCustomerName(customer.getName());
            bookingRes.setIdCard(customer.getIdCard());
            bookingRes.setRoomNumber(roomMapper.selectById(booking.getRoomID()).getRoomNumber());
            // 这里可以设置其他BookingRes属性
            bookingRes.setBookingTime(booking.getBookingTime());
            bookingRes.setCheckInTime(booking.getCheckInTime());
            bookingRes.setCheckOutTime(booking.getCheckOutTime());
            bookingRes.setTotalAmount(booking.getTotalAmount());
            bookingRes.setStatus(booking.getStatus());
            bookingRes.setBookingID(booking.getBookingID());
            bookingResList.add(bookingRes);
        }
        return bookingResList;
    }

    @Override
    public List<Booking> getBookedRooms() {
        return bookingMapper.selectBooked();
    }

    @Override
    public boolean checkIn(Booking booking) {
        int updated = bookingMapper.checkIn(booking.getBookingID(), "已入住");
        if (updated > 0) {
            roomMapper.updateStatus("已入住", booking.getRoomID());
            return true;
        }
        return false;
    }

    @Override
    public boolean checkOut(Booking booking) {
        int updated = bookingMapper.checkOut(booking.getBookingID(), "已退房");
        if (updated > 0) {
            roomMapper.updateStatus("空闲", booking.getRoomID());
            return true;
        }
        return false;
    }

    @Override
    public boolean cancel(Booking booking) {
        int updated = bookingMapper.updateStatus(booking.getBookingID(), "已取消");
        if (updated > 0) {
            roomMapper.updateStatus("空闲", booking.getRoomID());
            return true;
        }
        return false;
    }
}
